package communication.server.vertx.verticle;

import communication.common.business.base.EventBusManager;
import communication.common.business.base.FcrAutoConfig;
import communication.common.business.base.FcrProtocolConfig;
import communication.common.business.base.FcrSocketAddress;
import communication.common.business.util.OsParamUtil;
import communication.common.command.VertxCommand;
import communication.common.shell.FcrServerShell;
import communication.server.vertx.config.FcrServerAutoConfig;
import communication.common.vertx.net.NetManager;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;


/**
 *  server of vertx
 * @author mac
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class BusinessServerVerticle extends AbstractVerticle {

    private final NetManager netManager;

    private final VertxCommand vertxCommand;

    private final FcrServerShell fcrServerShell;

    private final EventBusManager eventBusManager;

    private final FcrAutoConfig fcrAutoConfig;


    @PostConstruct
    public void init() throws Exception {
        // CompositeFuture支持最多6个future
        CompositeFuture.all(
                // 装在自己的command
                vertxCommand.initCommand(),
                // 启动 ssh的shell
                fcrServerShell.startShellService(),
                // 初始化集群的eventBus
//                initClusterEventBus()
                createFcrServer()
        ).onSuccess(e->{
                    log.info("BusinessServerVerticle deploy finish!");
                })
        .onFailure(f->{
            log.error("BusinessServerVerticle deploy fail !");
            f.printStackTrace();
        });
    }

    private Future initClusterEventBus(){
        return Future.future(a->{
            eventBusManager.initClusterEventBus();
        });
    }




    private Future createFcrServer( ) throws Exception {
        // 获取无限网卡或有线网卡的ip地址
        String ip = OsParamUtil.getLocalIpAddress(null, null);
        if(StringUtils.isEmpty(ip)){
            throw new Exception("网卡Ip获取失败，无法监听指定端口");
        }

        FcrSocketAddress server = fcrAutoConfig.getServer();
        FcrSocketAddress local = new FcrSocketAddress(ip,server.getPort());

        switch (fcrAutoConfig.getProtocol()){
            case TCP:
                return netManager.createTcpServerFuture(ip,server.getPort())
                        .onSuccess(a->{
                            log.info("tcp socketServer start success! host:{},port:{}",ip,server.getPort());
                        })
                        .onFailure(f->{
                            log.warn("tcp socketServer start fail! host:{},port:{},error:{}",ip,server.getPort(),f.toString());
                        });

            case UDP:
                return netManager.createUdpServerFuture(local).future()
                        .onSuccess(a->{
                            log.info("udp socketServer start success! host:{},port:{}",ip,server);
                        })
                        .onFailure(f->{
                            log.warn("udp socketServer start fail! host:{},port:{},error:{}",ip,server,f.toString());
                        });
            default:
                throw new Exception("不支持的网络协议:"+server);

        }
    }


}
